//
//  UTAddConvenientLinkViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/8.
//

import UIKit

class UTAddConvenientLinkViewController: UTBaseViewController {

    var reloadData:(() -> Void)?
    
    @IBOutlet weak var linkNameTextField: UITextField!
    @IBOutlet weak var linkTextField: UITextField!
    @IBOutlet weak var yesButton: UIButton!
    @IBOutlet weak var noButton: UIButton!
    
    lazy var model: UTConnectorConvenientLinkModel = {
        let model = UTConnectorConvenientLinkModel()
        return model
    }()
    
    lazy var data: UTConnectorModel = {
        if let data = UTConnectorHelper.connectorInfo() {
            return data
        }
        return UTConnectorModel()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hexString: "#000000", alpha: 0.3)
    }

    @IBAction func didClose(_ sender: UIButton) {
        self.dismiss(animated: false)
    }
    
    @IBAction func didPlacementStatus(_ sender: UIButton) {
        if sender.isSelected { return }
        sender.isSelected = !sender.isSelected
        if sender == yesButton {
            noButton.isSelected = false
            model.isTop = true
        }else if sender == noButton {
            yesButton.isSelected = false
            model.isTop = false
        }
    }
    
    @IBAction func didSubmit(_ sender: UIButton) {
        if let name = linkNameTextField.text, name.isEmpty {
            self.view.makeToast("请输入链接名", position: .center)
            return
        }
        if let link = linkTextField.text, link.isEmpty, !link.contains("http") {
            self.view.makeToast("请输入正确链接", position: .center)
            return
        }
        model.icon = "icon_df"
        model.name = linkNameTextField.text ?? ""
        model.link = linkTextField.text ?? ""
        data.items.append(model)
        UTConnectorHelper.dc_saveTaskCounterInfo(connector: data)
        if let handler = reloadData {
            handler()
        }
        self.dismiss(animated: false)
    }
    
}
